﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FigureDraw;
using FigureDraw.Graphic;
using FigureDraw.Diagram;
using FigureDraw.Shapes;

namespace FigureDrawApp
{
    public partial class MainForm : Form
    {
        private CommonGraphic graphic;
        private MyPicture myPicture;
        private Point diem_dau;
        private Point diem_duoi;
        private MyShape current;
        private string loai;
        private string library;

        public MainForm()
        {            
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            myPicture = new MyPicture();
            loai = "Line";
            library = "GDI";           
        }

        private void Draw(CommonGraphic g, Point diem_dau, Point diem_duoi)
        {
            switch (loai)
            {
                case "Line":
                    g.DrawLine(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    break;
                case "Rectangle":
                    g.DrawRectangle(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    break;
                case "Ellipse":
                    g.DrawEllipse(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    break;
                case "Flower":                    
                    break;
                case "Star":                    
                    break;
                case "Text":
                    break;
                default:
                    break;
            }                        
        }

        private void btnGDIPlus_Click(object sender, EventArgs e)
        {
            library = "GDI";
            Invalidate();
        }

        private void btnCairo_Click(object sender, EventArgs e)      
        {
            library = "Cairo";
            Invalidate();
        }

        private void MainForm_Paint(object sender, PaintEventArgs e)
        {            
            switch (library)
            {
                case "GDI":
                    graphic = new GDIPlusScreenGraphic(e.Graphics);
                    break;
                case "Cairo":                    
                    graphic = new CairoScreenGraphic(e.Graphics);
                    break;
                default:
                    graphic = new GDIPlusScreenGraphic(e.Graphics);
                    break;
            }

            if (library == "Cairo")
            {
                foreach (MyShape item in myPicture.LstShape)
                {
                    item.Draw(graphic);
                }
                Draw(graphic, diem_dau, diem_duoi);
                e.Graphics.ReleaseHdc();
            }
            else
            {
                foreach (MyShape item in myPicture.LstShape)
                {
                    item.Draw(graphic);
                }
                Draw(graphic, diem_dau, diem_duoi);
            }
        }

        private void MainForm_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                diem_dau = e.Location;
            }
        }

        private void MainForm_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                diem_duoi = e.Location;
                Invalidate();
            }
        }

        private void MainForm_MouseUp(object sender, MouseEventArgs e)
        {
            switch (loai)
            {
                case "Line":
                    current = new MyLine(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    myPicture.Add(current);
                    break;
                case "Rectangle":
                    current = new MyRectangle(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    myPicture.Add(current);
                    break;
                case "Ellipse":
                    current = new MyEllipse(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    myPicture.Add(current);
                    break;
                case "Flower":
                    current = new MyFlower(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    myPicture.Add(current);
                    break;
                case "Star":
                    current = new MyStar(diem_dau.X, diem_dau.Y, diem_duoi.X, diem_duoi.Y);
                    myPicture.Add(current);
                    break;
                case "Text":                    
                    break;
                default:
                    break;
            }                        
        }

        private void btn_DrawLine_Click(object sender, EventArgs e)
        {
            loai = "Line";
        }

        private void btn_DrawRec_Click(object sender, EventArgs e)
        {
            loai = "Rectangle";
        }

        private void btn_DrawEllipse_Click(object sender, EventArgs e)
        {
            loai = "Ellipse";
        }

        private void btn_DrawFlower_Click(object sender, EventArgs e)
        {
            loai = "Flower";
        }

        private void btn_DrawStar_Click(object sender, EventArgs e)
        {
            loai = "Star";
        }

        private void btn_DrawText_Click(object sender, EventArgs e)
        {
            loai = "Text";
        }
    }
}
